# coding=utf-8

from functools import partial
from qtpy import QtWidgets, QtCore

class MenuButton(QtWidgets.QPushButton):
    def __init__(self, *args):
        super(MenuButton, self).__init__(*args)
        self._menu = None
        self._actions = []

    def set_menu_enabled(self, status):
        if not status:
            self._menu = QtWidgets.QMenu()
            self._menu.setStyleSheet("""QMenu{
                                            color:rgb(200,200,200);
                                            font: 9pt \"Arial\";
                                            background:rgb(35,35,35);
                                            border: 1px solid rgb(0, 120, 215);
                                            }
                                        QMenu:item:disabled{
                                            color:rgba(50,50,50);
                                            }
                                        QMenu:item:disabled:selected{
                                            background:rgba(0, 120, 215, 30);
                                            color:rgba(50,50,50);
                                            }
                                        QMenu:item:selected{
                                            background:rgb(0, 120, 215);
                                            }

                                            """)
        else:
            self._menu = None
        self.setMenu(self._menu)
        # self.setLayoutDirection(QtCore.Qt.LayoutDirection.RightToLeft)

    def set_actions(self, actions, hook, data):
        """
        添加菜单
        """
        if not self._menu:
            return
        self._actions = actions
        name_list = self._actions.keys()
        for name in name_list:
            cmd = self._actions[name]
            action = QtWidgets.QAction(name, self)
            action.triggered.connect(partial(hook.main, cmd, data))
            self._menu.addAction(action)

